/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_DB_DATABASE_H_
#define SHARED_DB_DATABASE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#include <memory>
#include <boost/unordered_map.hpp>
#include "Shared/Global_Macros.h"
#include "Shared/Base/Mutex.h"
#include "Shared/DB/SqlPreparedStatement.h"


namespace Shared {
namespace DB {
    class QueryResult;
    class SqlResultQueue;
    class SqlDelayThread;

    class Database
    {
    public:
        const static int MAX_QUERY_LENGTH = 32 * 1024;

    public:
        Database(void);
        virtual ~Database(void);

    public:
        static bool checkSanity(void);

        virtual bool initialize(const std::string &host,
            const std::string &port, const std::string &user,
            const std::string &pass, const std::string &db, 
            bool autoReconnect = true);
        virtual void close(void);

        virtual void escapeString(std::string &sql);

        virtual QueryResult *pquery(const char *sql, ...);
        virtual bool pexecute(const char *sql, ...);
        virtual void ping(void) = 0;

        bool error(void) const;
        const std::string &errorString(void) const;

        ///////////////////////////////////////////////////////////////////
        SqlStatement createStatement(SqlStatementID& id, const char *format);
        bool executeStmt(const SqlStatementID& id, SqlStmtParameters* params);
        bool directExecuteStmt(const SqlStatementID& id, SqlStmtParameters* params);
        std::string getStmtString(const int stmtId) const;


        ///////////////////////////////////////////////////////////////////
        void processResultQueue(void);


        ///////////////////////////////////////////////////////////////////
        template<class Class, typename ParamType1>
        bool asyncQuery(Class* object, void (Class::*method)
            (QueryResult*, ParamType1), ParamType1 param1, const char* sql);

        template<class Class, typename ParamType1>
        bool asyncPQuery(Class* object, void (Class::*method)
            (QueryResult*, ParamType1), ParamType1 param1, const char* format, ...);

    protected:
        virtual bool reconnect(void) = 0;


        bool _allowAsyncTransaction;
        std::auto_ptr<SqlConnection> _conn;
        std::auto_ptr<SqlConnection> _asyncConn;

        typedef boost::unordered_map<std::string, int> PreparedStmtRegistry;
        PreparedStmtRegistry _stmtRegistry;
        mutable Mutex _stmtRegistryLock;
        int _stmtIndex;

        std::auto_ptr<SqlResultQueue> _resultQueue;
        std::auto_ptr<SqlDelayThread> _delayThread;
        DISALLOW_COPY_AND_ASSIGN(Database);
    };

}}

#endif // SHARED_DB_DATABASE_H_
